@echo on
chcp 65001 >nul
echo ========================================
echo    电商平台微服务启动脚本
echo ========================================
echo.

:: 设置项目根目录
set PROJECT_ROOT=F:\shopping
echo 项目根目录: %PROJECT_ROOT%

:: 检查Java环境
echo 检查Java环境...
java -version
if %ERRORLEVEL% NEQ 0 (
    echo 错误: 未找到Java环境，请确保已安装Java并配置了JAVA_HOME环境变量
    pause
    exit /b 1
)
echo Java环境检查通过

:: 检查Maven环境
echo 检查Maven环境...
mvn -version
if %ERRORLEVEL% NEQ 0 (
    echo 错误: 未找到Maven环境，请确保已安装Maven并配置了MAVEN_HOME环境变量
    pause
    exit /b 1
)
echo Maven环境检查通过

echo 正在编译项目...
echo.

:: 编译common-module
echo [1/6] 编译common-module...
cd %PROJECT_ROOT%\common-module
echo 当前目录: %CD%
call mvn clean install -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: common-module编译失败
    pause
    exit /b 1
)
echo common-module编译成功

:: 编译auth-service
echo [2/6] 编译auth-service...
cd %PROJECT_ROOT%\auth-service
echo 当前目录: %CD%
call mvn clean package -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: auth-service编译失败
    pause
    exit /b 1
)
echo auth-service编译成功

:: 编译user-service
echo [3/6] 编译user-service...
cd %PROJECT_ROOT%\user-service
echo 当前目录: %CD%
call mvn clean package -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: user-service编译失败
    pause
    exit /b 1
)
echo user-service编译成功

:: 编译product-service
echo [4/6] 编译product-service...
cd %PROJECT_ROOT%\product-service
echo 当前目录: %CD%
call mvn clean package -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: product-service编译失败
    pause
    exit /b 1
)
echo product-service编译成功

:: 编译order-service
echo [5/6] 编译order-service...
cd %PROJECT_ROOT%\order-service
echo 当前目录: %CD%
call mvn clean package -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: order-service编译失败
    pause
    exit /b 1
)
echo order-service编译成功

:: 编译gateway-service
echo [6/6] 编译gateway-service...
cd %PROJECT_ROOT%\gateway-service
echo 当前目录: %CD%
call mvn clean package -DskipTests
if %ERRORLEVEL% NEQ 0 (
    echo 错误: gateway-service编译失败
    pause
    exit /b 1
)
echo gateway-service编译成功

echo.
echo 所有模块编译成功！
echo.
echo 正在启动服务...
echo.

:: 启动auth-service
echo 启动auth-service...
start "auth-service" cmd /k "chcp 65001 >nul ^&^& cd /d %PROJECT_ROOT%\auth-service ^&^& java -jar target\auth-service-1.0.0.jar"
timeout /t 5 /nobreak >nul

:: 启动user-service
echo 启动user-service...
start "user-service" cmd /k "chcp 65001 >nul ^&^& cd /d %PROJECT_ROOT%\user-service ^&^& java -jar target\user-service-1.0.0.jar"
timeout /t 5 /nobreak >nul

:: 启动product-service
echo 启动product-service...
start "product-service" cmd /k "chcp 65001 >nul ^&^& cd /d %PROJECT_ROOT%\product-service ^&^& java -jar target\product-service-1.0.0.jar"
timeout /t 5 /nobreak >nul

:: 启动order-service
echo 启动order-service...
start "order-service" cmd /k "chcp 65001 >nul ^&^& cd /d %PROJECT_ROOT%\order-service ^&^& java -jar target\order-service-1.0.0.jar"
timeout /t 5 /nobreak >nul

:: 启动gateway-service
echo 启动gateway-service...
start "gateway-service" cmd /k "chcp 65001 >nul ^&^& cd /d %PROJECT_ROOT%\gateway-service ^&^& java -jar target\gateway-service-1.0.0.jar"

echo.
echo ========================================
echo    所有服务已启动！
echo ========================================
echo.
echo 服务端口：
echo - auth-service:    通常为8081
echo - user-service:    通常为8082
echo - product-service: 通常为8083
echo - order-service:   通常为8084
echo - gateway-service: 通常为8080
echo.
echo 按任意键退出...
pause >nul